package com.cheng.reggie.service.impl;

import com.cheng.reggie.common.R;
import com.cheng.reggie.entity.Employee;
import com.cheng.reggie.entity.Page;
import com.cheng.reggie.exception.BusinessException;
import com.cheng.reggie.mapper.EmployeeMapper;
import com.cheng.reggie.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.ExecutorService;
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;

    /**
     * @param employee
     * @return R R对象存放的用户登录数据
     */
    @Override
    public Employee login(Employee employee) {
        //1.校验用户是否存在
        //调用mapper根据用户名查找用户对象
        Employee loginEmployee = employeeMapper.login(employee);

        //判断用户是否为null,如果为null说明用户名不存在,提示用户
        if(loginEmployee == null){
            throw new BusinessException("用户名或者密码错误");
        }

        //2.校验密码是否正确
        //对用户输入的明文密码加密
        String md5 = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        //再与数据库密文对比
        if (!loginEmployee.getPassword().equals(md5)) {
            throw new BusinessException("用户名或者密码错误");
        }
        //3.校验状态是否禁用
        if (loginEmployee.getStatus()==0){
            throw new BusinessException("你的账号被禁用,请联系管理员解决");
        }
        return loginEmployee;
    }

    /**
     * 新增用户方法
     *
     * @param employee
     * @return R
     */
    @Override
    public boolean save(Employee employee) {
        //1.根据账号username唯一
        //根据账号查询用户
        Employee dbEmployee = employeeMapper.login(employee);

        //判断dbEmployee是否为null,如果不为null,说明账号,提示用户
        if (dbEmployee != null) {
            throw  new BusinessException("账号已被注册请修改");
        }

        //2.完善数据
        //默认密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //状态
        employee.setStatus(1);
        //创建时间
        employee.setCreateTime(LocalDateTime.now());
        //修改时间
        employee.setUpdateTime(LocalDateTime.now());

        //调用mapper添加数据库
        employeeMapper.save(employee);

        return true;
    }

    /**
     * 分页查询根据
     *
     * @param page 当前页数
     * @param pageSize 页面大小
     * @param name 用户名
     * @return Page
     */
    @Override
    public Page<Employee> findByPage(Integer page, Integer pageSize, String name) {
        //1.设置分页查询第几页和每页多少条
        PageHelper.startPage(page,pageSize);
        //2.查询sql
        List<Employee> employeeList = employeeMapper.findByName(name);
        //3.创建一个pageInfo封装上面的结果集
        PageInfo<Employee> pageInfo = new PageInfo<Employee>(employeeList);

        Page<Employee> pageObj = new Page<>();
        pageObj.setRecords(pageInfo.getList());
        pageObj.setTotal(pageInfo.getTotal());

        return pageObj;
    }

    /**
     * 修改员工
     *
     * @param employee
     * @return boolean
     */
    @Override
    public boolean updata(Employee employee) {

        if (employee.getUsername()!=employee.getUsername()) {
            employee.setUpdateTime(LocalDateTime.now());
            Employee login = employeeMapper.login(employee);
            if (login != null) {
                throw new BusinessException("用户已注册,请重新修改!");
            }
        }


        //调用mapper的修改sql语句
        Integer update = employeeMapper.update(employee);
        return update>0;
    }

    /**
     * 编辑数据
     *
     * @param id
     * @return boolean
     */
    @Override
    public Employee findById(Long id) {
        //调用mapper编辑sql语句
        Employee byId = employeeMapper.findById(id);

        return byId;
    }
}
